package com.ycsj.goldmedalnewconcept;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.iflytek.cloud.SpeechUtility;
import com.ycsj.goldmedalnewconcept.pickerview.dao.DBManager;

import cn.jpush.android.api.JPushInterface;
import io.rong.imkit.RongIM;

@SuppressLint("NewApi")
public class App extends MultiDexApplication {
    private DBManager dbHelper;


    @Override
    public void onCreate() {

        super.onCreate();

        //j-push
        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush

        JPushInterface.getRegistrationID(this);
        Log.i("push", JPushInterface.getRegistrationID(this));

//        SPUtils.put(this, Constant.JPUSH_ID, JPushInterface.getRegistrationID(this));


        //导入数据库
        dbHelper = new DBManager(this);
        dbHelper.openDatabase();

//        SpeechUtility.createUtility(App.this, "appid=" + getString(R.string.app_id));

        SpeechUtility.createUtility(this, "appid=" + "57be937a");


        /**
         * OnCreate 会被多个进程重入，这段保护代码，确保只有您需要使用 RongIM 的进程和 Push 进程执行了 init。
         * io.rong.push 为融云 push 进程名称，不可修改。
         *  ||
         "io.rong.push".equals(getCurProcessName(getApplicationContext()))
         */
        if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {

            /**
             * IMKit SDK调用第一步 初始化
             */
            RongIM.init(this);
            Log.i("LoginActivity", "RongIM.init(this);");
        }


        //初始化化okHttp
//        initOKhttp();
    }


    /**
     * 初始化OkHttpUtils
     */
//    private void initOKhttp() {
//        //配置支持Https请求
//        //配置Https证书
//        try {
//            //支持指定证书认证的Https请求
////            HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(new InputStream[
////                    ]{getAssets().open("https.cer")}, null, null);
//            //支持所有的Https请求
//            HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
//            CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
//            OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                    .addInterceptor(new LoggerInterceptor("TAG"))
//                    .connectTimeout(5000L, TimeUnit.MILLISECONDS)
//                    .readTimeout(5000L, TimeUnit.MILLISECONDS)
//                    .cookieJar(cookieJar)
//                    .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
//                    //其他配置
//                    .hostnameVerifier(new HostnameVerifier() {
//                        @Override
//                        public boolean verify(String hostname, SSLSession session) {
//                            return true;
//                        }
//                    })
//                    .build();
//            OkHttpUtils.initClient(okHttpClient);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }


    /**
     * 获得当前进程的名字
     *
     * @param context
     * @return 进程号
     */
    @SuppressLint("NewApi")
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

}